package com.axend.sofa.ui.activity

import android.app.Activity
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.axend.lib_base.ext.click
import com.axend.lib_base.help.GlideEngine
import com.axend.lib_base.livedatabus.LiveDatabus
import com.axend.lib_base.utils.ActivityManage
import com.axend.lib_service.ARouterPath
import com.axend.lib_service.Constant
import com.axend.sofa.R
import com.axend.sofa.databinding.ActivityScanQrcodeBinding
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.yxing.ScanCodeActivity
import com.yxing.ScanCodeConfig
import com.yxing.utils.QrCodeUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


@Route(path = ARouterPath.ACTIVITY_SCAN_QR_CODE)
class ScanQRCodeActivity : ScanCodeActivity() {

    private lateinit var binding: ActivityScanQrcodeBinding
    private var isOpenFlash = false


    override fun getLayoutView(): View {
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityScanQrcodeBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
    }

    override fun initData() {
        super.initData()
        ActivityManage.addActivity(this)
        /*标题粘性事件*/
        LiveDatabus.getInstance().withSticky(Constant.EVENT_BUS_KEY_SCAN_TITLE, String::class.java)
            .observe(this) { title ->
                binding.titleBar.setTitle(title)
            }
        ImmersionBar.with(this).apply {
            hideBar(BarHide.FLAG_HIDE_STATUS_BAR)
        }
        /*camera亮度值监听*/
        setScanCodeAnalyzerListener {
            lifecycleScope.launch(Dispatchers.Main) {
                if (it < 10f || isOpenFlash) {
                    binding.ivLight.visibility = View.VISIBLE
                } else {
                    binding.ivLight.visibility = View.GONE
                }
            }
        }
        binding.ivLight.click {
            isOpenFlash = !isOpenFlash
            binding.ivLight.setImageResource(
                if (isOpenFlash) com.axend.lib_base.R.drawable.vector_light_on
                else com.axend.lib_base.R.drawable.vector_light_off
            )
            setFlashStatus(isOpenFlash)
        }
        binding.ivAlbum.click {
            showAlbum()
        }
        binding.tvAlbum.click {
            showAlbum()
        }


    }


    private fun showAlbum() {
        PictureSelector.create(this)
            .openGallery(SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine())
            .setMaxSelectNum(1)
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: ArrayList<LocalMedia?>) {
                    lifecycleScope.launch {
                        val code = withContext(Dispatchers.IO) {
                            QrCodeUtil.scanningImage(
                                this@ScanQRCodeActivity,
                                Uri.parse(result[0]?.path)
                            )
                        }
                        intent.putExtra(ScanCodeConfig.CODE_KEY, code)
                        setResult(Activity.RESULT_OK, intent)
                        ActivityManage.finishActivity(this@ScanQRCodeActivity)
                    }

                }

                override fun onCancel() {}
            })
    }
}